﻿using App.TestApi.Models;
using Microsoft.EntityFrameworkCore;

namespace App.TestApi
{

    public class AppTestApiDbContext : DbContext {

        public AppTestApiDbContext(DbContextOptions<AppTestApiDbContext> options)
        : base(options)
        {
        }


        public DbSet<Todo> Todo { get; set; }


        public override int SaveChanges()
        {
            //自动修改 CreateTime,UpdateTime
            var entityEntries = ChangeTracker.Entries().ToList();
            foreach (var entry in entityEntries)
            {
                var model = Entry(entry.Entity);
                if (entry.State == EntityState.Added)
                {
                    model.Property("CreateDt").CurrentValue = DateTime.Now;
                }
                if (entry.State == EntityState.Modified)
                {
                    model.Property("UpdateDt").CurrentValue = DateTime.Now;
                }
            }

            return base.SaveChanges();
        }
    }
}
